package cc.mrbird.febs.utils;

import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.*;

import java.awt.*;
import java.util.*;
import java.util.List;

public class StyleMap {
    // 分表样式
    public static Map<String, XSSFCellStyle> getStyleMap(XSSFWorkbook workbook) {
        return new HashMap<String, XSSFCellStyle>() {{
            for (Entry<String, Color> entry : YearMonthMapUtils.getSubDetailColor.entrySet()) {
                XSSFCellStyle xssfCellStyle = workbook.createCellStyle();

                XSSFFont font = workbook.createFont();
                font.setFontHeightInPoints((short) 11); // 字体大小
                font.setColor(new XSSFColor(new java.awt.Color(0, 0, 0), new DefaultIndexedColorMap())); // 字体颜色
                font.setFontName("宋体");

                xssfCellStyle.setFont(font);
                setBorder(xssfCellStyle);
                Color color = entry.getValue();
                XSSFColor xssfColor = new XSSFColor(color, new DefaultIndexedColorMap());
                xssfCellStyle.setFillForegroundColor(xssfColor);
                xssfCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
                put(entry.getKey(), xssfCellStyle);
            }
        }};
    }

    ;

    // 分表默认样式
    public static XSSFCellStyle getDefaultStyle(XSSFWorkbook workbook) {
        XSSFCellStyle xssfCellStyle = workbook.createCellStyle();
        XSSFFont font = workbook.createFont();
        font.setFontHeightInPoints((short) 11); // 字体大小
        font.setColor(new XSSFColor(new java.awt.Color(0, 0, 0), new DefaultIndexedColorMap())); // 字体颜色
        font.setFontName("宋体");
        xssfCellStyle.setFont(font);
        setBorder(xssfCellStyle);
        return xssfCellStyle;
    }

    // 主表字体
    public static HashMap<String, XSSFFont> getMainTableFontMap(XSSFWorkbook workbook) {
        return new HashMap<String, XSSFFont>() {{
            // 标题字体
            XSSFFont font = workbook.createFont();
            font.setFontHeightInPoints((short) 10);
            font.setFontName("宋体");
            font.setBold(true);
            font.setColor(new XSSFColor(new java.awt.Color(0, 0, 0), new DefaultIndexedColorMap()));
            put("title", font);
            // 蓝色标题字体
            font = workbook.createFont();
            font.setFontHeightInPoints((short) 10);
            font.setFontName("宋体");
            font.setBold(true);
            font.setColor(new XSSFColor(new java.awt.Color(25, 63, 231), new DefaultIndexedColorMap()));
            put("blueTitle", font);
            // 一般字体
            font = workbook.createFont();
            font.setFontHeightInPoints((short) 10);
            font.setFontName("宋体");
            font.setColor(new XSSFColor(new java.awt.Color(0, 0, 0), new DefaultIndexedColorMap()));
            put("common", font);
            //  一般红色字体
            font = workbook.createFont();
            font.setFontHeightInPoints((short) 10);
            font.setFontName("宋体");
            font.setColor(new XSSFColor(new java.awt.Color(255, 0, 0), new DefaultIndexedColorMap()));
            put("redCommon", font);
        }};
    }

    // 主表填充颜色
    public final static Map<String, Color> getFillColor = new HashMap<String, Color>() {{
        Color blueColor = new Color(154, 203, 255);
        Color redColor = new Color(255, 153, 204);
        Color ziColor = new Color(204, 153, 255);
        Color shengZi = new Color(178, 105, 255);
        Color huiColor = new Color(192, 192, 192);
        Color huangColor = new Color(250, 191, 143);
        Color yellowColor = new Color(246, 210, 137);
        put("projectName", blueColor);
        put("formula", blueColor);
        put("月份", huiColor);
        put("总科目", huiColor);
        put("明细科目", huiColor);
        put("实际", blueColor);
        put("预算", huiColor);
        put("20*年月均数", huiColor);
        put("与全年预算差额（节约负数列示）", huiColor);
        put("实际合计（包括实际发生及预提）", blueColor);
        put("全年合计", huiColor);
        put("预算合计", huiColor);
        put("差额", huiColor);
        put("本部年终奖预提（不包括各项目管理人员）", redColor);
        put("收入合计", blueColor);
        put("成本合计", blueColor);
        put("税费合计", blueColor);
        put("费用合计", blueColor);
        put("收入", ziColor);
        put("费用", huiColor);
        put("成本", ziColor);
        put("税费", huiColor);
//        put("进项", redColor);
//        put("进项税额", redColor);
//        put("暂估", huiColor);
//        put("税费暂估（包括在关联公司已交税金）", huiColor);
        put("毛利润", huangColor);
        put("利润总额", yellowColor);
        put("所得税", huiColor);
        put("净利润", ziColor);
        put("净利润率", shengZi);
        put("员工人数", blueColor);
    }};

    // 第一行样式
    public static XSSFCellStyle getTitleStyle(XSSFWorkbook workbook) {
        XSSFCellStyle titleStyle = workbook.createCellStyle();      // 单元格样式
        titleStyle.setAlignment(HorizontalAlignment.CENTER);        // 设置水平居中
        titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);  // 设置垂直居中
        XSSFFont titleFont = workbook.createFont();  // 创建字体
        titleFont.setFontHeightInPoints((short) 18); // 设置字体大小
        titleFont.setColor(new XSSFColor(new java.awt.Color(0, 0, 0), new DefaultIndexedColorMap())); // 设置字体颜色
        titleFont.setFontName("宋体");
        titleFont.setBold(true);  // 加粗
        titleStyle.setFont(titleFont);   // 设置字体样式
        return titleStyle;
    }

    // 第二行样式
    public static XSSFCellStyle getSecondRowStyle1(XSSFWorkbook workbook) {
        XSSFCellStyle titleStyle = workbook.createCellStyle();
        XSSFColor xssfColor = new XSSFColor(new java.awt.Color(141, 180, 226));  // 设置科目，明确科目的颜色
        titleStyle.setAlignment(HorizontalAlignment.CENTER);
        return MainRowStyle(workbook, titleStyle, xssfColor,14, "宋体", 1);
    }

    public static XSSFCellStyle getSecondRowStyle2(XSSFWorkbook workbook) {
        XSSFCellStyle titleStyle = workbook.createCellStyle();
        XSSFColor xssfColor = new XSSFColor(new java.awt.Color(242, 220, 219));  // 设置普通科目的颜色
        titleStyle.setAlignment(HorizontalAlignment.CENTER);
        return MainRowStyle(workbook, titleStyle, xssfColor, 11, "宋体", 0);
    }

    public static XSSFCellStyle getSecondRowStyle3(XSSFWorkbook workbook) {
        XSSFCellStyle titleStyle = workbook.createCellStyle();
        XSSFColor xssfColor = new XSSFColor(new java.awt.Color(204, 192, 218));  // 设置类型总计的颜色
        titleStyle.setAlignment(HorizontalAlignment.CENTER);
        return MainRowStyle(workbook, titleStyle, xssfColor,11, "宋体", 0);
    }

    public static XSSFCellStyle getSecondRowStyle4(XSSFWorkbook workbook) {
        XSSFCellStyle titleStyle = workbook.createCellStyle();
        XSSFColor xssfColor = new XSSFColor(new java.awt.Color(146, 205, 220));  // 设置年总计的颜色
        titleStyle.setAlignment(HorizontalAlignment.CENTER);
        return MainRowStyle(workbook, titleStyle, xssfColor, 11, "宋体", 0);
    }

    // 第一列样式
    public static XSSFCellStyle getColumnStyle1(XSSFWorkbook workbook) {
        XSSFCellStyle style = workbook.createCellStyle();
        XSSFColor xssfColor = new XSSFColor(new Color(154, 203, 255));  // 设置颜色
        style.setAlignment(HorizontalAlignment.CENTER);
        return MainRowStyle(workbook, style, xssfColor, 11, "微软雅黑", 1);
    }
    // 第二列样式
    public static XSSFCellStyle getColumnStyle2(XSSFWorkbook workbook) {
        XSSFCellStyle style = workbook.createCellStyle();
        XSSFColor xssfColor = new XSSFColor(new Color(255, 192, 0));  // 设置颜色
        style.setAlignment(HorizontalAlignment.LEFT);
        return MainRowStyle(workbook, style, xssfColor, 10, "微软雅黑", 1);
    }
    // 主列样式
    public static XSSFCellStyle getColumnStyle3(XSSFWorkbook workbook, String code) {
        XSSFCellStyle style = workbook.createCellStyle();
        XSSFFont font = workbook.createFont();
        font.setFontHeightInPoints((short) 10); // 字体大小
        font.setColor(new XSSFColor(new java.awt.Color(0,0,0), new DefaultIndexedColorMap())); // 字体颜色
        font.setFontName("宋体");
        if(Objects.equals(code, CodeConstant.totalOutlayCode) || Objects.equals(code, CodeConstant.totalCostCode)){
            font.setBold(true);
        }
        style.setFont(font);
        setBorder(style);
        return style;
    }
    // 表内类型样式
    public static XSSFCellStyle getColumnStyle4(XSSFWorkbook workbook, String code) {
        XSSFCellStyle titleStyle = workbook.createCellStyle();
        XSSFColor xssfColor = new XSSFColor(new java.awt.Color(204, 192, 218));  // 设置类型总计的颜色
        return MainRowStyle2(workbook, titleStyle, xssfColor, 10, "宋体", code);
    }
    // 最后一列样式
    public static XSSFCellStyle getColumnStyle5(XSSFWorkbook workbook, String code) {
        XSSFCellStyle style = workbook.createCellStyle();
        XSSFColor xssfColor = new XSSFColor(new Color(146, 205, 220));  // 设置颜色
        return MainRowStyle2(workbook, style, xssfColor, 11, "宋体", code);
    }
    // 利润列样式
    public static XSSFCellStyle getProfitColumnStyle(XSSFWorkbook workbook) {
        XSSFCellStyle style = workbook.createCellStyle();
        XSSFColor xssfColor = new XSSFColor(new Color(230, 184, 183));  // 设置颜色
        style.setAlignment(HorizontalAlignment.LEFT);
        return MainRowStyle(workbook, style, xssfColor, 10, "微软雅黑", 1);
    }
    // 利润第一行样式
    public static XSSFCellStyle getProfitRowStyle1(XSSFWorkbook workbook) {
        XSSFCellStyle style = workbook.createCellStyle();
        XSSFColor xssfColor = new XSSFColor(new Color(191, 191, 191));  // 设置颜色
        return MainRowStyle(workbook, style, xssfColor, 10, "宋体", 1);
    }
    // 利润第二行样式
    public static XSSFCellStyle getProfitRowStyle2(XSSFWorkbook workbook) {
        XSSFCellStyle style = workbook.createCellStyle();
        XSSFColor xssfColor = new XSSFColor(new Color(221, 217, 196));  // 设置颜色
        return MainRowStyle(workbook, style, xssfColor, 10, "宋体", 0);
    }
    // 利润第三行毛利率样式
    public static XSSFCellStyle getProfitRowStyle3(XSSFWorkbook workbook) {
        XSSFCellStyle style = workbook.createCellStyle();
        XSSFColor xssfColor = new XSSFColor(new Color(250, 191, 143));  // 设置颜色
        return MainRowStyle(workbook, style, xssfColor, 10, "宋体", 0);
    }
    // 管理成本毛利润样式
    public static XSSFCellStyle getProfitRowStyle4(XSSFWorkbook workbook) {
        XSSFCellStyle style = workbook.createCellStyle();
        XSSFColor xssfColor = new XSSFColor(new Color(216, 228, 188));  // 设置颜色
        return MainRowStyle(workbook, style, xssfColor, 10, "宋体", 1);
    }
    // 管理成本毛利率样式
    public static XSSFCellStyle getProfitRowStyle5(XSSFWorkbook workbook) {
        XSSFCellStyle style = workbook.createCellStyle();
        XSSFColor xssfColor = new XSSFColor(new Color(218, 238, 243));  // 设置颜色
        return MainRowStyle(workbook, style, xssfColor, 10, "宋体", 0);
    }
//    项目未开票明细科目表样式
    public static XSSFCellStyle getIncomeSecondRow1(XSSFWorkbook workbook) {
        XSSFCellStyle style = workbook.createCellStyle();
        return addStyle(workbook, style, 11, "宋体", 1,1, 0);
    }
    public static XSSFCellStyle getIncomeSecondRow2(XSSFWorkbook workbook) {
        XSSFCellStyle style = workbook.createCellStyle();
        return addStyle(workbook, style, 11, "宋体", 0,2, 0);
    }
    public static XSSFCellStyle getIncomeSecondRow3(XSSFWorkbook workbook) {
        XSSFCellStyle style = workbook.createCellStyle();
        return addStyle(workbook, style, 11, "宋体", 1,3, 0);
    }


    public static XSSFCellStyle MainRowStyle(XSSFWorkbook workbook, XSSFCellStyle titleStyle, XSSFColor xssfColor, int size, String fontName, int bold){
        titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        XSSFFont fontTitle = workbook.createFont();
        fontTitle.setFontHeightInPoints((short)size); // 字体大小
        fontTitle.setColor(new XSSFColor(new java.awt.Color(0, 0,0), new DefaultIndexedColorMap())); // 字体颜色
        fontTitle.setFontName(fontName);   // 字体类型
        if(bold==1)
            fontTitle.setBold(true);       // 是否加粗
        titleStyle.setFont(fontTitle);     // 设置字体样式
        setBorder(titleStyle);
        titleStyle.setFillForegroundColor(xssfColor);    // 设置填充颜色
        titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);  // 设置填充样式
        titleStyle.setWrapText(true);       // 自动换行
        return titleStyle;
    }

    public static XSSFCellStyle MainRowStyle2(XSSFWorkbook workbook, XSSFCellStyle titleStyle, XSSFColor xssfColor, int size, String fontName, String code){
        titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        XSSFFont fontTitle = workbook.createFont();
        fontTitle.setFontHeightInPoints((short)size); // 字体大小
        fontTitle.setColor(new XSSFColor(new java.awt.Color(255, 249, 210), new DefaultIndexedColorMap())); // 字体颜色
        fontTitle.setFontName(fontName);   // 字体类型
        if(Objects.equals(code, CodeConstant.totalOutlayCode) || Objects.equals(code, CodeConstant.totalCostCode))
            fontTitle.setBold(true);       // 是否加粗
        titleStyle.setFont(fontTitle);     // 设置字体样式
        setBorder(titleStyle);
        titleStyle.setFillForegroundColor(xssfColor);    // 设置填充颜色
        titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);  // 设置填充样式
        titleStyle.setWrapText(true);       // 自动换行
        return titleStyle;
    }
    private static void setBorder(XSSFCellStyle style) {
        style.setBorderBottom(BorderStyle.THIN); //下边框
        style.setBorderLeft(BorderStyle.THIN);//左边框
        style.setBorderTop(BorderStyle.THIN);//上边框
        style.setBorderRight(BorderStyle.THIN);//右边框
    }
    public static XSSFCellStyle addStyle(XSSFWorkbook workbook, XSSFCellStyle style, int size, String fontName, int bold, int flag, int borderflag){
        if (flag == 1) {
            style.setAlignment(HorizontalAlignment.LEFT);
        } else if(flag == 2) {
            style.setAlignment(HorizontalAlignment.CENTER);        // 设置水平居中
        } else if(flag == 3) {
            style.setAlignment(HorizontalAlignment.RIGHT);
        }
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        XSSFFont font = workbook.createFont();
        font.setFontHeightInPoints((short)size); // 字体大小
        font.setFontName(fontName);   // 字体类型
        if(bold==1)
            font.setBold(true);       // 是否加粗
        style.setFont(font);     // 设置字体样式
        style.setWrapText(true);       // 自动换行
        if(borderflag == 1) {
            setBorder(style);
        }
        return style;
    }

}
